首页 > 试题广场 >

验证回文字符串(二)

[编程题]验证回文字符串(二)
  • 热度指数:1490 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个字符串,请问最多删除一个的情况下,能否组成一个回文字符串。

回文字符串:正着读和反着读是一样的字符串。

数据范围:字符串长度满足 ,字符串中仅包含小写英文字母
示例1

输入

"nowwon"

输出

true
示例2

输入

"nowewon"

输出

true
示例3

输入

"noweawon"

输出

true
示例4

输入

"noowwwn"

输出

false
class Solution:
    def palindrome(self , str: str) -> bool:
        # write code here
        return str == str[::-1]

发表于 2022-07-03 16:42:35 回复(0)
class Solution:
    def palindrome(self , str: str) -> bool:
        # write code here
        count = 0
        s = str[::-1]
        for i in range(len(s)):
            if s[i] != str[i]:
                count += 1
        if count > 2: return False
        return True

发表于 2022-04-22 17:09:49 回复(0)
class Solution:
    def palindrome(self , str: str) -> bool:
        # write code here
        n=len(str)
        count=0
        left=0
        right=n-1
        while left<right:
            if(str[left]!=str[right]):
                count+=1
            left+=1
            right-=1
        if count>=2:
            return False
        else:
            return True
发表于 2022-04-17 22:48:09 回复(0)